home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 #1 / Ham Radio 2000.iso / ham2000 / tcp_ip / tnos / tnos100s / socket.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-30  |  47.7 KB  |  2,067 lines

  1. /* Application programming interface routines - based loosely on the
  2.  * "socket" model in Berkeley UNIX.
  3.  *
  4.  * Copyright 1991 Phil Karn, KA9Q
  5.  */
  6. #include <stdio.h>
  7. #include <time.h>
  8. #ifdef  __STDC__
  9. #include <stdarg.h>
  10. #endif
  11. #include "global.h"
  12. #include "config.h"
  13. #include "mbuf.h"
  14. #include "netuser.h"
  15. #include "timer.h"
  16. #include "iface.h"
  17. #include "ip.h"
  18. #include "tcp.h"
  19. #include "udp.h"
  20. #include "ax25.h"
  21. #include "lapb.h"
  22. #include "netrom.h"
  23. #include "nr4.h"
  24. #include "proc.h"
  25. #include "lzw.h"
  26. #include "usock.h"
  27. #include "socket.h"
  28.  
  29. /* In connect(), accept() and recv_mbuf()
  30.  * the noblock options have been removed, since they never occur anyway.
  31.  * If you want then back, do a '#define useblock 1'
  32.  * The socket's 'noblock' parameter is now only effective for sending
  33.  * data; ie if set, only sending data will not block. - WG7J
  34.  */
  35.  
  36.  
  37. static void autobind __ARGS((int s,int af));
  38. static int checkaddr __ARGS((int type,char *name,int namelen));
  39. static void rip_recv __ARGS((struct raw_ip *rp));
  40. static void s_trcall __ARGS((struct tcb *tcb,int cnt));
  41. static void s_tscall __ARGS((struct tcb *tcb,int old,int new));
  42. static void s_ttcall __ARGS((struct tcb *tcb,int cnt));
  43. static void s_urcall __ARGS((struct iface *iface,struct udp_cb *udp,int cnt));
  44. static void trdiscard __ARGS((struct tcb *tcb,int cnt));
  45. #ifdef    NETROM
  46. static void s_nrcall __ARGS((struct nr4cb *cb,int cnt));
  47. static void s_nscall __ARGS((struct nr4cb *cb,int old,int new));
  48. static void s_ntcall __ARGS((struct nr4cb *cb,int cnt));
  49. #endif
  50.  
  51. static int16 Lport = 1024;
  52.  
  53. char *Socktypes[] = {
  54.     "Not Used",
  55.     "TCP",
  56.     "UDP",
  57.     "AX25 I",
  58.     "AX25 UI",
  59.     "Raw IP",
  60.     "NETROM3",
  61.     "NETROM",
  62.     "Loc St",
  63.     "Loc Dg"
  64. };
  65. char Badsocket[] = "Bad socket";
  66. struct usock *Usock;        /* Socket entry array */
  67. int Nusock = DEFNSOCK;        /* Number of socket entries */
  68.  
  69. /* The following two variables are needed because there can be only one
  70.  * socket listening on each of the AX.25 modes (I and UI)
  71.  */
  72. #ifdef    AX25
  73. int Axi_sock = -1;    /* Socket number listening for AX25 connections */
  74.  
  75. #ifdef notdef
  76. static int Axui_sock = -1;  /* Socket number listening for AX25 UI frames */
  77. static struct mbuf *Bcq;    /* Queue of incoming UI frames */
  78.  
  79. /* Function that handles incoming UI frames from lapb.c */
  80. void
  81. beac_input(iface,src,bp)
  82. struct iface *iface;
  83. char *src;
  84. struct mbuf *bp;
  85. {
  86.     struct mbuf *hdr;
  87.     struct sockaddr_ax *sax;
  88.  
  89.     if(Axui_sock == -1){
  90.         /* Nobody there to read it */
  91.         free_p(bp);
  92.     } else {
  93.         if((hdr = pushdown(NULLBUF,sizeof(struct sockaddr_ax))) == NULLBUF){
  94.             free_p(bp);
  95.             return;
  96.         }
  97.         sax = (struct sockaddr_ax *)hdr->data;
  98.         sax->sax_family = AF_AX25;
  99.         memcpy(sax->ax25_addr,src,AXALEN);
  100.         strncpy(sax->iface,iface->name,ILEN);
  101.         hdr->next = bp;
  102.         enqueue(&Bcq,hdr);
  103.     }
  104. }
  105. #endif
  106. #endif /* AX.25 */
  107.  
  108. /* Initialize user socket array */
  109. void
  110. sockinit()
  111. {
  112.     if(Usock != NULLUSOCK)
  113.         return;    /* Already initialized */
  114.     Usock = (struct usock *)callocw(Nusock,sizeof(struct usock));
  115. }
  116.  
  117. /* Create a user socket, return socket index
  118.  * The mapping to actual protocols is as follows:
  119.  *        
  120.  *        
  121.  * ADDRESS FAMILY    Stream        Datagram    Raw        Seq. Packet
  122.  *
  123.  * AF_INET        TCP        UDP        IP
  124.  * AF_AX25        I-frames    UI-frames
  125.  * AF_NETROM                        NET/ROM L3  NET/ROM L4
  126.  * AF_LOCAL        stream loopback    packet loopback
  127.  */
  128. int
  129. socket(af,type,protocol)
  130. int af;        /* Address family */
  131. int type;    /* Stream or datagram */
  132. int protocol;    /* Used for raw IP sockets */
  133. {
  134.     register struct usock *up;
  135.     int s;
  136.  
  137.     for(up=Usock;up < &Usock[Nusock];up++)
  138.         if(up->type == NOTUSED)
  139.             break;
  140.  
  141.     if(up == &Usock[Nusock]){
  142.         /* None left */
  143.         errno = EMFILE;
  144.         return -1;
  145.     }
  146.     /* set the time the socket was created - WG7J */
  147.     up->created = time(NULL);
  148.     up->refcnt = 1;
  149.     s = up - Usock + SOCKBASE;
  150.     errno = 0;
  151.     up->noblock = 0;
  152.     up->rdysock = -1;
  153.     up->cb.p = NULLCHAR;
  154.     up->peername = up->name = NULLCHAR;
  155.     up->namelen = up->peernamelen = 0;
  156.     up->owner = Curproc;
  157.     up->obuf = NULLBUF;
  158.     memset(up->errcodes,0,sizeof(up->errcodes));
  159.     memset(up->eol,0,sizeof(up->eol));
  160.     up->flush = '\n';    /* default is line buffered */
  161.     switch(af){
  162.     case AF_LOCAL:
  163.         up->cb.local = (struct loc *) callocw(1,sizeof(struct loc));
  164.         up->cb.local->peer = up;    /* connect to self */
  165.         switch(type){
  166.         case SOCK_STREAM:
  167.             up->type = TYPE_LOCAL_STREAM;
  168.             up->cb.local->hiwat = LOCSFLOW;
  169.             break;
  170.         case SOCK_DGRAM:
  171.             up->type = TYPE_LOCAL_DGRAM;
  172.             up->cb.local->hiwat = LOCDFLOW;
  173.             break;
  174.         default:
  175.             free(up->cb.local);
  176.             errno = ESOCKTNOSUPPORT;
  177.             break;
  178.         }
  179.         break;
  180. #ifdef    AX25
  181.     case AF_AX25:
  182.         switch(type){
  183.         case SOCK_STREAM:
  184.             up->type = TYPE_AX25I;
  185.             break;
  186.         case SOCK_DGRAM:
  187.             up->type = TYPE_AX25UI;
  188.             break;
  189.         default:
  190.             errno = ESOCKTNOSUPPORT;
  191.             break;
  192.         }
  193.         strcpy(up->eol,AX_EOL);
  194.         break;
  195. #endif
  196. #ifdef NETROM
  197.     case AF_NETROM:
  198.         switch(type){
  199.         case SOCK_RAW:
  200.             up->type = TYPE_NETROML3;
  201.             up->cb.rnr = raw_nr((char)protocol);
  202.             break;
  203.         case SOCK_SEQPACKET:
  204.             up->type = TYPE_NETROML4;
  205.             break;
  206.         default:
  207.             errno = ESOCKTNOSUPPORT;
  208.             break;
  209.         }
  210.         strcpy(up->eol,AX_EOL);
  211.         break;
  212. #endif
  213.     case AF_INET:
  214.         switch(type){
  215.         case SOCK_STREAM:
  216.             up->type = TYPE_TCP;
  217.             strcpy(up->eol,INET_EOL);
  218.             break;
  219.         case SOCK_DGRAM:
  220.             up->type = TYPE_UDP;
  221.             break;
  222.         case SOCK_RAW:
  223.             up->type = TYPE_RAW;
  224.             up->cb.rip = raw_ip(protocol,rip_recv);
  225.                         up->cb.rip->user = s;
  226.             break;
  227.         default:
  228.             errno = ESOCKTNOSUPPORT;
  229.             break;
  230.         }
  231.         break;
  232.     default:
  233.         errno = EAFNOSUPPORT;
  234.         break;
  235.     }
  236.     if(errno)
  237.         return -1;
  238.  
  239.     return s;
  240. }
  241.  
  242. /* Attach a local address/port to a socket. If not issued before a connect
  243.  * or listen, will be issued automatically
  244.  */
  245. int
  246. bind(s,name,namelen)
  247. int s;        /* Socket index */
  248. char *name;    /* Local name */
  249. int namelen;    /* Length of name */
  250. {
  251.     register struct usock *up;
  252.     union sp local;
  253.     struct socket lsock;
  254.  
  255.     if((up = itop(s)) == NULLUSOCK){
  256.         errno = EBADF;
  257.         return -1;
  258.     }
  259.     if(up->type == TYPE_LOCAL_STREAM || up->type == TYPE_LOCAL_DGRAM){
  260.         errno = EINVAL;
  261.         return -1;
  262.     }
  263.     if(name == NULLCHAR){
  264.         errno = EFAULT;
  265.         return -1;
  266.     }
  267.     if(up->name != NULLCHAR){
  268.         /* Bind has already been issued */
  269.         errno = EINVAL;
  270.         return -1;
  271.     }
  272.     if(checkaddr(up->type,name,namelen) == -1){
  273.         /* Incorrect length or family for chosen protocol */
  274.         errno = EAFNOSUPPORT;
  275.         return -1;    
  276.     }
  277.     /* Stash name in an allocated block */
  278.     up->namelen = namelen;
  279.     up->name = mallocw(namelen);
  280.     memcpy(up->name,name,namelen);
  281.     /* Create control block for datagram sockets */
  282.     switch(up->type){
  283.     case TYPE_UDP:
  284.         local.in = (struct sockaddr_in *)up->name;
  285.         lsock.address = local.in->sin_addr.s_addr;
  286.         lsock.port = local.in->sin_port;
  287.         up->cb.udp = open_udp(&lsock,s_urcall);
  288.         up->cb.udp->user = s;
  289.         break;
  290. #ifdef  notdef
  291.     case TYPE_AX25UI:
  292.         if(Axui_sock != -1){
  293.             errno = EADDRINUSE;
  294.             return -1;
  295.         }
  296.         Axui_sock = s;
  297.         break;
  298. #endif
  299.     }
  300.     return 0;
  301. }
  302. /* Post a listen on a socket */
  303. int
  304. listen(s,backlog)
  305. int s;        /* Socket index */
  306. int backlog;    /* 0 for a single connection, 1 for multiple connections */
  307. {
  308.     register struct usock *up;
  309.     union sp local;
  310.     struct socket lsock;
  311.  
  312.     if((up = itop(s)) == NULLUSOCK){
  313.         errno = EBADF;
  314.         return -1;
  315.     }
  316.     if(up->type == TYPE_LOCAL_STREAM || up->type == TYPE_LOCAL_DGRAM){
  317.         errno = EINVAL;
  318.         return -1;
  319.     }
  320.     if(up->cb.p != NULLCHAR){
  321.         errno = EISCONN;
  322.         return -1;
  323.     }
  324.     switch(up->type){
  325.     case TYPE_TCP:
  326.         if(up->name == NULLCHAR)
  327.             autobind(s,AF_INET);
  328.  
  329.         local.in = (struct sockaddr_in *)up->name;
  330.         lsock.address = local.in->sin_addr.s_addr;
  331.         lsock.port = local.in->sin_port;
  332.         up->cb.tcb = open_tcp(&lsock,NULLSOCK,
  333.          backlog ? TCP_SERVER:TCP_PASSIVE,0,
  334.         s_trcall,s_ttcall,s_tscall,0,s);
  335.         break;
  336. #ifdef AX25
  337.     case TYPE_AX25I:
  338.         if(up->name == NULLCHAR)
  339.             autobind(s,AF_AX25);
  340.         if(s != Axi_sock){
  341.             errno = EOPNOTSUPP;
  342.             return -1;
  343.         }
  344.         local.ax = (struct sockaddr_ax *)up->name;
  345.         up->cb.ax25 = open_ax25(NULLIF,local.ax->ax25_addr,NULLCHAR,
  346.          backlog ? AX_SERVER:AX_PASSIVE,0,
  347.          s_arcall,s_atcall,s_ascall,s);
  348.         break;
  349. #endif
  350. #ifdef NETROM
  351.     case TYPE_NETROML4:
  352.         if(up->name == NULLCHAR)
  353.             autobind(s,AF_NETROM);
  354.         local.nr = (struct sockaddr_nr *)up->name;
  355.         up->cb.nr4 = open_nr4(&local.nr->nr_addr,NULLNRADDR,
  356.          backlog ? AX_SERVER:AX_PASSIVE,
  357.          s_nrcall,s_ntcall,s_nscall,s);
  358.         break;
  359. #endif
  360.     default:
  361.         /* Listen not supported on datagram sockets */
  362.         errno = EOPNOTSUPP;
  363.         return -1;        
  364.     }
  365.     return 0;
  366. }
  367.  
  368. /* Initiate active open. For datagram sockets, merely bind the remote address. */
  369. int
  370. connect(s,peername,peernamelen)
  371. int s;            /* Socket index */
  372. char *peername;        /* Peer name */
  373. int peernamelen;    /* Length of peer name */
  374. {
  375.     register struct usock *up;
  376.     union cb cb;
  377.     union sp local,remot;
  378.     struct socket lsock,fsock;
  379. #ifdef    AX25
  380.     struct iface *iface;
  381. #endif
  382.  
  383.     if(availmem() < Memthresh){  /* Not enough resources - WG7J */
  384.         errno = ENOMEM;
  385.         return -1;
  386.     }
  387.     if((up = itop(s)) == NULLUSOCK){
  388.         errno = EBADF;
  389.         return -1;
  390.     }
  391.     if(up->type == TYPE_LOCAL_DGRAM || up->type == TYPE_LOCAL_STREAM){
  392.         errno = EINVAL;
  393.         return -1;
  394.     }
  395.     if(peername == NULLCHAR){
  396.         /* Connect must specify a remote address */
  397.         errno = EFAULT;
  398.         return -1;
  399.     }
  400.     if(checkaddr(up->type,peername,peernamelen) == -1){
  401.         errno = EAFNOSUPPORT;
  402.         return -1;
  403.     }
  404.     /* Raw socket control blocks are created in socket() */
  405.     if(up->type != TYPE_RAW && up->type != TYPE_NETROML3 &&
  406.        up->cb.p != NULLCHAR){
  407.         errno = EISCONN;
  408.         return -1;
  409.     }
  410.     up->peername = mallocw(peernamelen);
  411.     memcpy(up->peername,peername,peernamelen);
  412.     up->peernamelen = peernamelen;
  413.  
  414.     /* Set up the local socket structures */
  415.     if(up->name == NULLCHAR){
  416.         switch(up->type){
  417.         case TYPE_TCP:
  418.         case TYPE_UDP:
  419.         case TYPE_RAW:
  420.             autobind(s,AF_INET);
  421.             break;
  422. #ifdef    AX25
  423.         case TYPE_AX25I:
  424.         case TYPE_AX25UI:
  425.             autobind(s,AF_AX25);
  426.             break;
  427. #endif
  428. #ifdef    NETROM
  429.         case TYPE_NETROML3:
  430.         case TYPE_NETROML4:
  431.             autobind(s,AF_NETROM);
  432.             break;
  433. #endif
  434.         }
  435.     }
  436.     switch(up->type){
  437.     case TYPE_TCP:
  438.         /* Construct the TCP-style ports from the sockaddr structs */
  439.         local.in = (struct sockaddr_in *)up->name;
  440.         remot.in = (struct sockaddr_in *)up->peername;
  441.  
  442.         if(local.in->sin_addr.s_addr == INADDR_ANY)
  443.             /* Choose a local address */
  444.             local.in->sin_addr.s_addr =
  445.              locaddr(remot.in->sin_addr.s_addr);
  446.  
  447.         lsock.address = local.in->sin_addr.s_addr;
  448.         lsock.port = local.in->sin_port;
  449.         fsock.address = remot.in->sin_addr.s_addr;
  450.         fsock.port = remot.in->sin_port;
  451.  
  452.         /* Open the TCB in active mode */
  453.         up->cb.tcb = open_tcp(&lsock,&fsock,TCP_ACTIVE,0,
  454.          s_trcall,s_ttcall,s_tscall,0,s);
  455.  
  456.         /* Wait for the connection to complete */
  457.         while((cb.tcb = up->cb.tcb) != NULLTCB && cb.tcb->state != TCP_ESTABLISHED){
  458. #ifdef useblock
  459.             if(up->noblock){
  460.                 errno = EWOULDBLOCK;
  461.                 return -1;
  462.             } else
  463. #endif
  464.             if((errno = pwait(up)) != 0){
  465.                 return -1;
  466.             }
  467.         }
  468.         if(cb.tcb == NULLTCB){
  469.             /* Probably got refused */
  470.             free(up->peername);
  471.             up->peername = NULLCHAR;
  472.             errno = ECONNREFUSED;
  473.             return -1;
  474.         }
  475.         break;
  476.     case TYPE_UDP:
  477. #ifdef    AX25
  478.     case TYPE_AX25UI:
  479. #endif
  480. #ifdef    NETROM
  481.     case TYPE_NETROML3:
  482. #endif
  483.     case TYPE_RAW:
  484.         /* Control block already created by bind() */
  485.         break;
  486. #ifdef    AX25
  487.     case TYPE_AX25I:
  488.         local.ax = (struct sockaddr_ax *)up->name;
  489.         remot.ax = (struct sockaddr_ax *)up->peername;
  490.         if((iface = if_lookup(remot.ax->iface)) == NULLIF){
  491.             errno = EINVAL;
  492.             return -1;
  493.         }
  494.         if(local.ax->ax25_addr[0] == '\0'){
  495.             /* The local address was unspecified; set it from
  496.              * the interface address
  497.              */
  498.             memcpy(local.ax->ax25_addr,iface->hwaddr,AXALEN);
  499.         }
  500.         /* If we already have an AX25 link we can use it */
  501.         if((up->cb.ax25 = find_ax25(local.ax->ax25_addr, \
  502.                                     remot.ax->ax25_addr,iface)) != NULLAX25
  503.            && up->cb.ax25->state != LAPB_DISCONNECTED &&
  504.            up->cb.ax25->user == -1) {
  505.             up->cb.ax25->user = s;
  506.             up->cb.ax25->r_upcall = s_arcall;
  507.             up->cb.ax25->t_upcall = s_atcall;
  508.             up->cb.ax25->s_upcall = s_ascall;
  509.             if(up->cb.ax25->state == LAPB_CONNECTED
  510.                || up->cb.ax25->state == LAPB_RECOVERY)
  511.                     return 0;
  512.         } else
  513.             up->cb.ax25 = open_ax25(iface,local.ax->ax25_addr,
  514.              remot.ax->ax25_addr,AX_ACTIVE,
  515.              Axwindow,s_arcall,s_atcall,s_ascall,s);
  516.  
  517.         /* Wait for the connection to complete */
  518.         while((cb.ax25 = up->cb.ax25) != NULLAX25 && cb.ax25->state != LAPB_CONNECTED){
  519. #ifdef useblock
  520.             if(up->noblock){
  521.                 errno = EWOULDBLOCK;
  522.                 return -1;
  523.             } else
  524. #endif
  525.             if((errno = pwait(up)) != 0){
  526.                 return -1;
  527.             }
  528.         }
  529.         if(cb.ax25 == NULLAX25){
  530.             /* Connection probably already exists */
  531.             free(up->peername);
  532.             up->peername = NULLCHAR;
  533.             errno = ECONNREFUSED;
  534.             return -1;
  535.         }
  536.         break;
  537. #endif
  538. #ifdef    NETROM
  539.     case TYPE_NETROML4:
  540.         local.nr = (struct sockaddr_nr *)up->name;
  541.         remot.nr = (struct sockaddr_nr *)up->peername;
  542.         up->cb.nr4 = open_nr4(&local.nr->nr_addr,&remot.nr->nr_addr,
  543.          AX_ACTIVE,s_nrcall,s_ntcall,s_nscall,s);
  544.  
  545.         /* Wait for the connection to complete */
  546.         while((cb.nr4 = up->cb.nr4) != NULLNR4CB && cb.nr4->state != NR4STCON){
  547. #ifdef useblock
  548.             if(up->noblock){
  549.                 errno = EWOULDBLOCK;
  550.                 return -1;
  551.             } else
  552. #endif
  553.             if((errno = pwait(up)) != 0){
  554.                 return -1;
  555.             }
  556.         }
  557.         if(cb.nr4 == NULLNR4CB){
  558.             /* Connection probably already exists */
  559.             free(up->peername);
  560.             up->peername = NULLCHAR;
  561.             errno = ECONNREFUSED;
  562.             return -1;
  563.         }
  564.         break;
  565. #endif
  566.     }
  567.     return 0;
  568. }
  569. /* Wait for a connection. Valid only for connection-oriented sockets. */
  570. int
  571. accept(s,peername,peernamelen)
  572. int s;            /* Socket index */
  573. char *peername;        /* Peer name */
  574. int *peernamelen;    /* Length of peer name */
  575. {
  576.     int i;
  577.     register struct usock *up;
  578.  
  579.     if((up = itop(s)) == NULLUSOCK){
  580.         errno = EBADF;
  581.         return -1;
  582.     }
  583.     if(up->type == TYPE_LOCAL_DGRAM || up->type == TYPE_LOCAL_STREAM){
  584.         errno = EINVAL;
  585.         return -1;
  586.     }
  587.     if(up->cb.p == NULLCHAR){
  588.         errno = EOPNOTSUPP;
  589.         return -1;
  590.     }
  591.     /* Accept is valid only for stream sockets */
  592.     switch(up->type){
  593.     case TYPE_TCP:
  594. #ifdef    AX25
  595.     case TYPE_AX25I:
  596. #endif
  597. #ifdef    NETROM
  598.     case TYPE_NETROML4:
  599. #endif
  600.         break;
  601.     default:
  602.         errno = EOPNOTSUPP;
  603.         return -1;
  604.     }    
  605.     /* Wait for the state-change upcall routine to signal us */
  606.     while(up->cb.p != NULLCHAR && up->rdysock == -1){
  607. #ifdef useblock
  608.         if(up->noblock){
  609.             errno = EWOULDBLOCK;
  610.             return -1;
  611.         } else
  612. #endif
  613.         if((errno = pwait(up)) != 0){
  614.             return -1;
  615.         }
  616.     }
  617.     if(up->cb.p == NULLCHAR){
  618.         /* Blown away */
  619.         errno = EBADF;
  620.         return -1;
  621.     }
  622.     i = up->rdysock;
  623.     up->rdysock = -1;
  624.  
  625.     up = itop(i);
  626.     if(peername != NULLCHAR && peernamelen != NULL){
  627.         *peernamelen = min(up->peernamelen,*peernamelen);
  628.         memcpy(peername,up->peername,*peernamelen);
  629.     }
  630.     /* set the time this was created - WG7J */
  631.     up->created = time(NULL);
  632.     return i;
  633. }
  634. /* Low-level receive routine. Passes mbuf back to user; more efficient than
  635.  * higher-level functions recv() and recvfrom(). Datagram sockets ignore
  636.  * the len parameter.
  637.  */
  638. int
  639. recv_mbuf(s,bpp,flags,from,fromlen)
  640. int s;            /* Socket index */
  641. struct mbuf **bpp;    /* Place to stash receive buffer */
  642. int flags;        /* Unused; will control out-of-band data, etc */
  643. char *from;        /* Peer address (only for datagrams) */
  644. int *fromlen;        /* Length of peer address */
  645. {
  646.     register struct usock *up;
  647.     int cnt;
  648.     union cb cb;
  649.     struct socket fsocket;
  650. #ifdef    NETROM
  651.     struct nr3hdr n3hdr;
  652. #endif
  653.     union sp remot;
  654.     struct ip ip;
  655.     struct mbuf *bp;
  656.  
  657.     if((up = itop(s)) == NULLUSOCK){
  658.         errno = EBADF;
  659.         return -1;
  660.     }
  661.     if(up->ibuf != NULLBUF){
  662.         /* Return input buffer */
  663.         cnt = len_p(up->ibuf);
  664.         if(bpp != NULLBUFP)
  665.             *bpp = up->ibuf;
  666.         else
  667.             free_p(up->ibuf);
  668.         up->ibuf = NULLBUF;        
  669.         return cnt;
  670.     }
  671.     switch(up->type){
  672.     case TYPE_LOCAL_STREAM:
  673.     case TYPE_LOCAL_DGRAM:
  674.         while(up->cb.local != NULLLOC && up->cb.local->q == NULLBUF
  675.               && up->cb.local->peer != NULLUSOCK){
  676. #ifdef useblock
  677.             if(up->noblock){
  678.                 errno = EWOULDBLOCK;
  679.                 return -1;
  680.             } else
  681. #endif
  682.             if((errno = pwait(up)) != 0){
  683.                 return -1;
  684.             }
  685.         }
  686.         if(up->cb.local == NULLLOC){
  687.             errno = EBADF;
  688.             return -1;
  689.         }
  690.         if(up->cb.local->q == NULLBUF &&
  691.            up->cb.local->peer == NULLUSOCK){
  692.             errno = ENOTCONN;
  693.             return -1;
  694.         }
  695.         /* For datagram sockets, this will return the
  696.          * first packet on the queue. For stream sockets,
  697.          * this will return everything.
  698.          */
  699.         bp = dequeue(&up->cb.local->q);
  700.         if(up->cb.local->q == NULLBUF
  701.          && (up->cb.local->flags & LOC_SHUTDOWN))
  702.             close_s(s);
  703.         psignal(up,0);
  704.         cnt = len_p(bp);
  705.         break;
  706.     case TYPE_TCP:
  707.         while((cb.tcb = up->cb.tcb) != NULLTCB
  708.          && cb.tcb->r_upcall != trdiscard
  709.                  && (cnt = recv_tcp(cb.tcb,&bp,(int16)0)) == -1){
  710. #ifdef useblock
  711.             if(up->noblock){
  712.                 errno = EWOULDBLOCK;
  713.                 return -1;
  714.             } else
  715. #endif
  716.             if((errno = pwait(up)) != 0){
  717.                 return -1;
  718.             }
  719.         }
  720.         if(cb.tcb == NULLTCB){
  721.             /* Connection went away */
  722.             errno = ENOTCONN;
  723.             return -1;
  724.         } else if(cb.tcb->r_upcall == trdiscard){
  725.             /* Receive shutdown has been done */
  726.             errno = ENOTCONN;    /* CHANGE */
  727.             return -1;
  728.         }
  729.         break;
  730.     case TYPE_UDP:
  731.         while((cb.udp = up->cb.udp) != NULLUDP
  732.         && (cnt = recv_udp(cb.udp,&fsocket,&bp)) == -1){
  733. #ifdef useblock
  734.             if(up->noblock){
  735.                 errno = EWOULDBLOCK;
  736.                 return -1;
  737.             } else
  738. #endif
  739.             if((errno = pwait(up)) != 0){
  740.                 return -1;
  741.             }
  742.         }
  743.         if(cb.udp == NULLUDP){
  744.             /* Connection went away */
  745.             errno = ENOTCONN;
  746.             return -1;
  747.         }
  748.         if(from != NULLCHAR && fromlen != (int *)NULL && *fromlen >= SOCKSIZE){
  749.             remot.in = (struct sockaddr_in *)from;
  750.             remot.in->sin_family = AF_INET;
  751.             remot.in->sin_addr.s_addr = fsocket.address;
  752.             remot.in->sin_port = fsocket.port;
  753.             *fromlen = SOCKSIZE;
  754.         }
  755.         break;
  756.     case TYPE_RAW:
  757.         while((cb.rip = up->cb.rip) != NULLRIP
  758.          && cb.rip->rcvq == NULLBUF){
  759. #ifdef useblock
  760.             if(up->noblock){
  761.                 errno = EWOULDBLOCK;
  762.                 return -1;
  763.             } else
  764. #endif
  765.             if((errno = pwait(up)) != 0){
  766.                 return -1;
  767.             }
  768.         }
  769.         if(cb.rip == NULLRIP){
  770.             /* Connection went away */
  771.             errno = ENOTCONN;
  772.             return -1;
  773.         }
  774.         bp = dequeue(&cb.rip->rcvq);
  775.         ntohip(&ip,&bp);
  776.  
  777.         cnt = len_p(bp);
  778.         if(from != NULLCHAR && fromlen != (int *)NULL && *fromlen >= SOCKSIZE){
  779.             remot.in = (struct sockaddr_in *)from;
  780.             remot.in->sin_family = AF_INET;
  781.             remot.in->sin_addr.s_addr = ip.source;
  782.             remot.in->sin_port = 0;
  783.             *fromlen = SOCKSIZE;
  784.         }
  785.         break;
  786. #ifdef    AX25
  787.     case TYPE_AX25I:
  788.         while((cb.ax25 = up->cb.ax25) != NULLAX25
  789.          && (bp = recv_ax25(cb.ax25,(int16)0)) == NULLBUF){
  790. #ifdef useblock
  791.             if(up->noblock){
  792.                 errno = EWOULDBLOCK;
  793.                 return -1;
  794.             } else
  795. #endif
  796.             if((errno = pwait(up)) != 0){
  797.                 return -1;
  798.             }
  799.         }
  800.         if(cb.ax25 == NULLAX25){
  801.             /* Connection went away */
  802.             errno = ENOTCONN;
  803.             return -1;
  804.         }
  805.         cnt = bp->cnt;
  806.         break;
  807. #ifdef notdef
  808.     case TYPE_AX25UI:
  809.         while(s == Axui_sock && Bcq == NULLBUF){
  810. #ifdef useblock
  811.             if(up->noblock){
  812.                 errno = EWOULDBLOCK;
  813.                 return -1;
  814.             } else
  815. #endif
  816.             if((errno = pwait(&Bcq)) != 0){
  817.                 return -1;
  818.             }
  819.         }
  820.         if(s != Axui_sock){
  821.             errno = ENOTCONN;
  822.             return -1;
  823.         }
  824.         bp = dequeue(&Bcq);
  825.  
  826.         if(from != NULLCHAR && fromlen != NULLINT
  827.            && *fromlen >= sizeof(struct sockaddr_ax)){
  828.             pullup(&bp,from,sizeof(struct sockaddr_ax));
  829.             *fromlen = sizeof(struct sockaddr_ax);
  830.         } else {
  831.             pullup(&bp,NULLCHAR,sizeof(struct sockaddr_ax));
  832.         }
  833.         cnt = len_p(bp);
  834.         break;
  835. #endif /* notdef */
  836. #endif /* ax25 */
  837. #ifdef NETROM
  838.     case TYPE_NETROML3:
  839.         while((cb.rnr = up->cb.rnr) != NULLRNR
  840.          && cb.rnr->rcvq == NULLBUF){
  841. #ifdef useblock
  842.             if(up->noblock){
  843.                 errno = EWOULDBLOCK;
  844.                 return -1;
  845.             } else
  846. #endif
  847.             if((errno = pwait(up)) != 0){
  848.                 return -1;
  849.             }
  850.         }
  851.         if(cb.rnr == NULLRNR){
  852.             /* Connection went away */
  853.             errno = ENOTCONN;
  854.             return -1;
  855.         }
  856.         bp = dequeue(&cb.rnr->rcvq);
  857.         ntohnr3(&n3hdr,&bp);
  858.         cnt = len_p(bp);
  859.         if(from != NULLCHAR && fromlen != NULLINT
  860.            && *fromlen >= sizeof(struct sockaddr_nr)){
  861.             remot.nr = (struct sockaddr_nr *)from;
  862.             remot.nr->nr_family = AF_NETROM;
  863.             /* The callsign of the local user is not part of
  864.                NET/ROM level 3, so that field is not used here */
  865.             memcpy(remot.nr->nr_addr.node,n3hdr.source,AXALEN);
  866.             *fromlen = sizeof(struct sockaddr_nr);
  867.         }
  868.         break;
  869.     case TYPE_NETROML4:
  870.         while((cb.nr4 = up->cb.nr4) != NULLNR4CB
  871.          && (bp = recv_nr4(cb.nr4,(int16)0)) == NULLBUF){
  872. #ifdef useblock
  873.             if(up->noblock){
  874.                 errno = EWOULDBLOCK;
  875.                 return -1;
  876.             } else
  877. #endif
  878.             if((errno = pwait(up)) != 0){
  879.                 return -1;
  880.             }
  881.         }
  882.         if(cb.nr4 == NULLNR4CB){
  883.             /* Connection went away */
  884.             errno = ENOTCONN;
  885.             return -1;
  886.         }
  887.         cnt = bp->cnt;
  888.         break;
  889. #endif
  890.     }
  891.     if(bpp != NULLBUFP)
  892.         *bpp = bp;
  893.     else
  894.         free_p(bp);
  895.     return cnt;
  896. }
  897. /* Low level send routine; user supplies mbuf for transmission. More
  898.  * efficient than send() or sendto(), the higher level interfaces.
  899.  * The "to" and "tolen" parameters are ignored on connection-oriented
  900.  * sockets.
  901.  *
  902.  * In case of error, bp is freed so the caller doesn't have to worry about it.
  903.  */
  904. int
  905. send_mbuf(s,bp,flags,to,tolen)
  906. int s;            /* Socket index */
  907. struct mbuf *bp;    /* Buffer to send */
  908. int flags;        /* not currently used */
  909. char *to;        /* Destination, only for datagrams */
  910. int tolen;        /* Length of destination */
  911. {
  912.     register struct usock *up;
  913.     union cb cb;
  914.     union sp local,remot;
  915.     struct socket lsock,fsock;
  916.     int cnt;
  917.  
  918.     if((up = itop(s)) == NULLUSOCK){
  919.         free_p(bp);
  920.         errno = EBADF;
  921.         return -1;
  922.     }
  923. #ifndef    notdef
  924.     if(up->obuf != NULLBUF){
  925.         /* If there's unflushed output, send it.
  926.          * Note the importance of clearing up->obuf
  927.          * before the recursive call!
  928.          */
  929.         struct mbuf *bp1;
  930.  
  931.         bp1 = up->obuf;
  932.         up->obuf = NULLBUF;
  933.         send_mbuf(s,bp1,flags,to,tolen);
  934.     }
  935. #endif
  936.     if(up->name == NULLCHAR){
  937.         /* Automatic local name assignment for datagram sockets */
  938.         switch(up->type){
  939.         case TYPE_LOCAL_STREAM:
  940.         case TYPE_LOCAL_DGRAM:
  941.             break;    /* no op */
  942.         case TYPE_UDP:
  943.         case TYPE_RAW:
  944.             autobind(s,AF_INET);
  945.             break;
  946. #ifdef    AX25
  947.         case TYPE_AX25UI:
  948.             autobind(s,AF_AX25);
  949.             break;
  950. #endif
  951. #ifdef    NETROM
  952.         case TYPE_NETROML3:
  953.         case TYPE_NETROML4:
  954.             autobind(s,AF_NETROM);
  955.             break;
  956. #endif
  957.         default:
  958.             free_p(bp);
  959.             errno = ENOTCONN;
  960.             return -1;
  961.         }
  962.     }
  963.     cnt = len_p(bp);
  964.     switch(up->type){
  965.     case TYPE_LOCAL_DGRAM:
  966.         if(up->cb.local->peer == NULLUSOCK){
  967.             free_p(bp);
  968.             errno = ENOTCONN;
  969.             return -1;
  970.         }
  971.         enqueue(&up->cb.local->peer->cb.local->q,bp);
  972.         psignal(up->cb.local->peer,0);
  973.         /* If high water mark has been reached, block */
  974.         while(up->cb.local->peer != NULLUSOCK &&
  975.               len_q(up->cb.local->peer->cb.local->q) >=
  976.               up->cb.local->peer->cb.local->hiwat){
  977.             if(up->noblock){
  978.                 errno = EWOULDBLOCK;
  979.                 return -1;
  980.             } else if((errno = pwait(up->cb.local->peer)) != 0){
  981.                 return -1;
  982.             }
  983.         }
  984.         if(up->cb.local->peer == NULLUSOCK){
  985.             errno = ENOTCONN;
  986.             return -1;
  987.         }
  988.         break;
  989.     case TYPE_LOCAL_STREAM:
  990.         if(up->cb.local->peer == NULLUSOCK){
  991.             free_p(bp);
  992.             errno = ENOTCONN;
  993.             return -1;
  994.         }
  995.         append(&up->cb.local->peer->cb.local->q,bp);
  996.         psignal(up->cb.local->peer,0);
  997.         /* If high water mark has been reached, block */
  998.         while(up->cb.local->peer != NULLUSOCK &&
  999.               len_p(up->cb.local->peer->cb.local->q) >=
  1000.               up->cb.local->peer->cb.local->hiwat){
  1001.             if(up->noblock){
  1002.                 errno = EWOULDBLOCK;
  1003.                 return -1;
  1004.             } else if((errno = pwait(up->cb.local->peer)) != 0){
  1005.                 return -1;
  1006.             }
  1007.         }
  1008.         if(up->cb.local->peer == NULLUSOCK){
  1009.             errno = ENOTCONN;
  1010.             return -1;
  1011.         }
  1012.         break;
  1013.     case TYPE_TCP:
  1014.         if((cb.tcb = up->cb.tcb) == NULLTCB){
  1015.             free_p(bp);
  1016.             errno = ENOTCONN;
  1017.             return -1;
  1018.         }        
  1019.         cnt = send_tcp(cb.tcb,bp);
  1020.  
  1021.         while((cb.tcb = up->cb.tcb) != NULLTCB &&
  1022.          cb.tcb->sndcnt > cb.tcb->window){
  1023.             /* Send queue is full */
  1024.             if(up->noblock){
  1025.                 errno = EWOULDBLOCK;
  1026.                 return -1;
  1027.             } else if((errno = pwait(up)) != 0){
  1028.                 return -1;
  1029.             }
  1030.         }
  1031.          if(cb.tcb == NULLTCB){
  1032.             errno = ENOTCONN;
  1033.             return -1;
  1034.         }
  1035.         break;
  1036.     case TYPE_UDP:
  1037.         local.in = (struct sockaddr_in *)up->name;
  1038.         lsock.address = local.in->sin_addr.s_addr;
  1039.         lsock.port = local.in->sin_port;
  1040.         if(to != NULLCHAR)
  1041.             remot.in = (struct sockaddr_in *)to;
  1042.         else if(up->peername != NULLCHAR)
  1043.             remot.in = (struct sockaddr_in *)up->peername;
  1044.         else {
  1045.             errno = ENOTCONN;
  1046.             return -1;
  1047.         }    
  1048.         fsock.address = remot.in->sin_addr.s_addr;
  1049.         fsock.port = remot.in->sin_port;
  1050.         send_udp(&lsock,&fsock,0,0,bp,0,0,0);
  1051.         break;
  1052.     case TYPE_RAW:
  1053.         local.in = (struct sockaddr_in *)up->name;
  1054.         if(to != NULLCHAR)
  1055.             remot.in = (struct sockaddr_in *)to;
  1056.         else if(up->peername != NULLCHAR)
  1057.             remot.in = (struct sockaddr_in *)up->peername;
  1058.         else {
  1059.             errno = ENOTCONN;
  1060.             return -1;
  1061.         }    
  1062.         ip_send(local.in->sin_addr.s_addr,remot.in->sin_addr.s_addr,
  1063.             (char)up->cb.rip->protocol,0,0,bp,0,0,0);
  1064.         break;
  1065. #ifdef    AX25
  1066.     case TYPE_AX25I:
  1067.         if((cb.ax25 = up->cb.ax25) == NULLAX25){
  1068.             free_p(bp);
  1069.             errno = ENOTCONN;
  1070.             return -1;
  1071.         }
  1072.         send_ax25(cb.ax25,bp,PID_NO_L3);
  1073.  
  1074.         while((cb.ax25 = up->cb.ax25) != NULLAX25 &&
  1075.          len_q(cb.ax25->txq) * cb.ax25->paclen > cb.ax25->window){
  1076.             if(up->noblock){
  1077.                 errno = EWOULDBLOCK;
  1078.                 return -1;
  1079.             } else if((errno = pwait(up)) != 0){
  1080.                 return -1;
  1081.             }
  1082.         }
  1083.         if(cb.ax25 == NULLAX25){
  1084.             errno = EBADF;
  1085.             return -1;
  1086.         }
  1087.         break;
  1088.     case TYPE_AX25UI:
  1089.         local.ax = (struct sockaddr_ax *)up->name;
  1090.         if(to != NULLCHAR)
  1091.             remot.ax = (struct sockaddr_ax *)to;
  1092.         else if(up->peername != NULLCHAR)
  1093.             remot.ax = (struct sockaddr_ax *)up->peername;
  1094.         else {
  1095.             errno = ENOTCONN;
  1096.             return -1;
  1097.         }
  1098.         ax_output(if_lookup(remot.ax->iface),
  1099.               remot.ax->ax25_addr,
  1100.               local.ax->ax25_addr,PID_NO_L3,bp);
  1101.         break;
  1102. #endif
  1103. #ifdef NETROM
  1104.     case TYPE_NETROML3:
  1105.         /* This should never happen, since the TCP code will peek at the
  1106.          * interface mtu ! - WG7J
  1107.          */
  1108.         if(len_p(bp) > NR4MAXINFO) {
  1109.             free_p(bp);
  1110.             errno = EMSGSIZE;
  1111.             return -1;
  1112.         }
  1113.         local.nr = (struct sockaddr_nr *)up->name;
  1114.         if(to != NULLCHAR)
  1115.             remot.nr = (struct sockaddr_nr *)to;
  1116.         else if(up->peername != NULLCHAR)
  1117.             remot.nr = (struct sockaddr_nr *)up->peername;
  1118.         else {
  1119.             errno = ENOTCONN;
  1120.             return -1;
  1121.         }    
  1122.         /* The NETROM username is always ignored in outgoing traffic */
  1123.         nr_sendraw(remot.nr->nr_addr.node,
  1124.          up->cb.rnr->protocol,up->cb.rnr->protocol,bp);
  1125.         break;
  1126.     case TYPE_NETROML4:
  1127.         if((cb.nr4 = up->cb.nr4) == NULLNR4CB) {
  1128.             free_p(bp);
  1129.             errno = ENOTCONN;
  1130.             return -1;
  1131.         }
  1132. #ifdef notdef
  1133.         /* since I now fragment nr4 packets in send_nr4(),
  1134.          * don't worry, be happy :-) - WG7J
  1135.          */
  1136.         if(len_p(bp) > NR4MAXINFO){ /* reject big packets */
  1137.             free_p(bp);
  1138.             errno = EMSGSIZE;
  1139.             return -1;
  1140.         }
  1141. #endif
  1142.         send_nr4(cb.nr4,bp);
  1143.  
  1144.         while((cb.nr4 = up->cb.nr4) != NULLNR4CB &&
  1145.          cb.nr4->nbuffered >= cb.nr4->window){
  1146.             if(up->noblock){
  1147.                 errno = EWOULDBLOCK;
  1148.                 return -1;
  1149.             } else if((errno = pwait(up)) != 0){
  1150.                 errno = EINTR;
  1151.                 return -1;
  1152.             }
  1153.         }
  1154.         if(cb.nr4 == NULLNR4CB){
  1155.             errno = EBADF;
  1156.             return -1;
  1157.         }
  1158.         break;
  1159. #endif
  1160.     }    
  1161.     return cnt;
  1162. }
  1163. /* Return local name passed in an earlier bind() call */
  1164. int
  1165. getsockname(s,name,namelen)
  1166. int s;        /* Socket index */
  1167. char *name;    /* Place to stash name */
  1168. int *namelen;    /* Length of same */
  1169. {
  1170.     register struct usock *up;
  1171.  
  1172.     if((up = itop(s)) == NULLUSOCK){
  1173.         errno = EBADF;
  1174.         return -1;
  1175.     }
  1176.     if(name == NULLCHAR || namelen == (int *)NULL){
  1177.         errno = EFAULT;
  1178.         return -1;
  1179.     }
  1180.     if(up->name == NULLCHAR){
  1181.         /* Not bound yet */
  1182.         *namelen = 0;
  1183.         return 0;
  1184.     }
  1185.     if(up->name != NULLCHAR){
  1186.         *namelen = min(*namelen,up->namelen);
  1187.         memcpy(name,up->name,*namelen);
  1188.     }
  1189.     return 0;
  1190. }
  1191. /* Get remote name, returning result of earlier connect() call. */
  1192. int
  1193. getpeername(s,peername,peernamelen)
  1194. int s;            /* Socket index */
  1195. char *peername;        /* Place to stash name */
  1196. int *peernamelen;    /* Length of same */
  1197. {
  1198.     register struct usock *up;
  1199.  
  1200.     if((up = itop(s)) == NULLUSOCK){
  1201.         errno = EBADF;
  1202.         return -1;
  1203.     }
  1204.     if(up->peername == NULLCHAR){
  1205.         errno = ENOTCONN;
  1206.         return -1;
  1207.     }
  1208.     if(peername == NULLCHAR || peernamelen == (int *)NULL){
  1209.         errno = EFAULT;
  1210.         return -1;
  1211.     }
  1212.     *peernamelen = min(*peernamelen,up->peernamelen);
  1213.     memcpy(peername,up->peername,*peernamelen);
  1214.     return 0;
  1215. }
  1216. /* Return length of protocol queue, either send or receive. */
  1217. int
  1218. socklen(s,rtx)
  1219. int s;        /* Socket index */
  1220. int rtx;    /* 0 = receive queue, 1 = transmit queue */
  1221. {
  1222.     register struct usock *up;
  1223.     int len = -1;
  1224.  
  1225.     if((up = itop(s)) == NULLUSOCK){
  1226.         errno = EBADF;
  1227.         return -1;
  1228.     }
  1229.     if(up->cb.p == NULLCHAR){
  1230.         errno = ENOTCONN;
  1231.         return -1;
  1232.     }
  1233.     if(rtx < 0 || rtx > 1){
  1234.         errno = EINVAL;
  1235.         return -1;
  1236.     }
  1237.     switch(up->type){
  1238.     case TYPE_LOCAL_DGRAM:
  1239.         switch(rtx){
  1240.         case 0:
  1241.             len = len_q(up->cb.local->q);
  1242.             break;
  1243.         case 1:
  1244.             if(up->cb.local->peer != NULLUSOCK)
  1245.                 len = len_q(up->cb.local->peer->cb.local->q);
  1246.             break;
  1247.         }
  1248.         break;
  1249.     case TYPE_LOCAL_STREAM:
  1250.         switch(rtx){
  1251.         case 0:
  1252.             len = len_p(up->cb.local->q) + len_p(up->ibuf);
  1253.             break;
  1254.         case 1:
  1255.             if(up->cb.local->peer != NULLUSOCK)
  1256.                 len = len_p(up->cb.local->peer->cb.local->q)
  1257.                       + len_p(up->obuf);
  1258.             break;
  1259.         }
  1260.         break;
  1261.     case TYPE_TCP:
  1262.         switch(rtx){
  1263.         case 0:
  1264.             len = up->cb.tcb->rcvcnt + len_p(up->ibuf);
  1265.             break;
  1266.         case 1:
  1267.             len = up->cb.tcb->sndcnt + len_p(up->obuf);
  1268.             break;
  1269.         }
  1270.         break;
  1271.     case TYPE_UDP:
  1272.         switch(rtx){
  1273.         case 0:
  1274.             len = up->cb.udp->rcvcnt;
  1275.             break;
  1276.         case 1:
  1277.             len = 0;
  1278.             break;
  1279.         }
  1280.         break;
  1281. #ifdef    AX25
  1282.     case TYPE_AX25I:
  1283.         switch(rtx){
  1284.         case 0:
  1285.             len = len_p(up->cb.ax25->rxq) + len_p(up->ibuf);
  1286.             break;
  1287.         case 1:    /* Number of packets, not bytes */
  1288.             len = len_q(up->cb.ax25->txq);
  1289.             if(up->obuf != NULLBUF)
  1290.                 len++;
  1291.         }
  1292.         break;
  1293. #ifdef notdef
  1294.     case TYPE_AX25UI:
  1295.         switch(rtx){    
  1296.         case 0:
  1297.             len = len_q(Bcq);
  1298.             break;
  1299.         case 1:
  1300.             len = 0;        
  1301.             break;
  1302.         }
  1303.         break;
  1304. #endif /* notdef */
  1305. #endif
  1306. #ifdef NETROM
  1307.     case TYPE_NETROML3:
  1308.         switch(rtx){    
  1309.         case 0:
  1310.             len = len_q(up->cb.rnr->rcvq);
  1311.             break;
  1312.         case 1:
  1313.             len = 0;        
  1314.         }
  1315.         break;
  1316.     case TYPE_NETROML4:
  1317.         switch(rtx){
  1318.         case 0:
  1319.             len = len_p(up->cb.nr4->rxq) + len_p(up->ibuf);
  1320.             break;
  1321.         case 1:    /* Number of packets, not bytes */
  1322.             len = len_q(up->cb.nr4->txq);
  1323.             if(up->obuf != NULLBUF)
  1324.                 len++;
  1325.             break;
  1326.         }
  1327.         break;
  1328. #endif
  1329.     case TYPE_RAW:
  1330.         switch(rtx){    
  1331.         case 0:
  1332.             len = len_q(up->cb.rip->rcvq);
  1333.             break;
  1334.         case 1:
  1335.             len = 0;        
  1336.             break;
  1337.         }
  1338.         break;
  1339.     }
  1340.     return len;
  1341. }
  1342. /* Force retransmission. Valid only for connection-oriented sockets. */
  1343. int
  1344. sockkick(s)
  1345. int s;    /* Socket index */
  1346. {
  1347.     register struct usock *up;
  1348.  
  1349.     if((up = itop(s)) == NULLUSOCK){
  1350.         errno = EBADF;
  1351.         return -1;
  1352.     }
  1353.     if(up->type == TYPE_LOCAL_STREAM || up->type == TYPE_LOCAL_DGRAM){
  1354.         errno = EINVAL;
  1355.         return -1;
  1356.     }
  1357.     if(up->cb.p == NULLCHAR){
  1358.         errno = ENOTCONN;
  1359.         return -1;
  1360.     }
  1361.     switch(up->type){
  1362.     case TYPE_TCP:
  1363.         kick_tcp(up->cb.tcb);
  1364.         break;
  1365. #ifdef    AX25
  1366.     case TYPE_AX25I:
  1367.         kick_ax25(up->cb.ax25);
  1368.         break;
  1369. #endif
  1370. #ifdef NETROM
  1371.     case TYPE_NETROML4:
  1372.         kick_nr4(up->cb.nr4);
  1373.         break;
  1374. #endif
  1375.     default:
  1376.         /* Datagram sockets can't be kicked */
  1377.         errno = EOPNOTSUPP;
  1378.         return -1;
  1379.     }
  1380.     return 0;
  1381. }
  1382.  
  1383. /* Change owner of socket, return previous owner */
  1384. struct proc *
  1385. sockowner(s,newowner)
  1386. int s;            /* Socket index */
  1387. struct proc *newowner;    /* Process table address of new owner */
  1388. {
  1389.     register struct usock *up;
  1390.     struct proc *pp;
  1391.  
  1392.     if((up = itop(s)) == NULLUSOCK){
  1393.         errno = EBADF;
  1394.         return NULLPROC;
  1395.     }
  1396.     pp = up->owner;
  1397.     if(newowner != NULLPROC)
  1398.         up->owner = newowner;
  1399.     return pp;
  1400. }
  1401. /* Close down a socket three ways. Type 0 means "no more receives"; this
  1402.  * replaces the incoming data upcall with a routine that discards further
  1403.  * data. Type 1 means "no more sends", and obviously corresponds to sending
  1404.  * a TCP FIN. Type 2 means "no more receives or sends". This I interpret
  1405.  * as "abort the connection".
  1406.  */
  1407. int
  1408. shutdown(s,how)
  1409. int s;        /* Socket index */
  1410. int how;    /* (see above) */
  1411. {
  1412.     register struct usock *up;
  1413.  
  1414.     if((up = itop(s)) == NULLUSOCK){
  1415.         errno = EBADF;
  1416.         return -1;
  1417.     }
  1418.     if(up->cb.p == NULLCHAR){
  1419.         errno = ENOTCONN;
  1420.         return -1;
  1421.     }
  1422.     switch(up->type){
  1423.     case TYPE_LOCAL_DGRAM:
  1424.     case TYPE_LOCAL_STREAM:
  1425.         if(up->cb.local->q == NULLBUF)
  1426.             close_s(s);
  1427.         else
  1428.             up->cb.local->flags = LOC_SHUTDOWN;
  1429.         break;
  1430.     case TYPE_TCP:
  1431.         switch(how){
  1432.         case 0:    /* No more receives -- replace upcall */
  1433.             up->cb.tcb->r_upcall = trdiscard;
  1434.             break;
  1435.         case 1:    /* Send EOF */
  1436.             close_tcp(up->cb.tcb);
  1437.             break;
  1438.         case 2:    /* Blow away TCB */
  1439.             reset_tcp(up->cb.tcb);
  1440.             up->cb.tcb = NULLTCB;
  1441.             break;
  1442.         }
  1443.         break;
  1444. #ifdef    AX25
  1445. #ifdef notdef
  1446.     case TYPE_AX25UI:
  1447.         close_s(s);
  1448.         break;
  1449. #endif
  1450.     case TYPE_AX25I:
  1451.         switch(how){
  1452.         case 0:
  1453.         case 1:    /* Attempt regular disconnect */
  1454.             disc_ax25(up->cb.ax25);
  1455.             break;
  1456.         case 2: /* Blow it away */
  1457.             reset_ax25(up->cb.ax25);
  1458.             up->cb.ax25 = NULLAX25;
  1459.             break;
  1460.         }
  1461.         break;        
  1462. #endif
  1463. #ifdef    NETROM
  1464.     case TYPE_NETROML3:
  1465.         close_s(s);
  1466.         break;
  1467.     case TYPE_NETROML4:
  1468.         switch(how){
  1469.         case 0:
  1470.         case 1:    /* Attempt regular disconnect */
  1471.             disc_nr4(up->cb.nr4);
  1472.             break;
  1473.         case 2: /* Blow it away */
  1474.             reset_nr4(up->cb.nr4);
  1475.             up->cb.nr4 = NULLNR4CB;
  1476.             break;
  1477.         }
  1478.         break;        
  1479. #endif
  1480.     case TYPE_RAW:
  1481.     case TYPE_UDP:
  1482.         close_s(s);
  1483.         break;
  1484.     default:
  1485.         errno = EOPNOTSUPP;
  1486.         return -1;
  1487.     }
  1488.     psignal(up,0);
  1489.     return 0;
  1490. }
  1491. /* Close a socket, freeing it for reuse. Try to do a graceful close on a
  1492.  * TCP socket, if possible
  1493.  */
  1494. int
  1495. close_s(s)
  1496. int s;        /* Socket index */
  1497. {
  1498.     register struct usock *up;
  1499.  
  1500.     if((up = itop(s)) == NULLUSOCK){
  1501.         errno = EBADF;
  1502.         return -1;
  1503.     }
  1504.     if(--up->refcnt > 0)
  1505.         return 0;    /* Others are still using it */
  1506.     usflush(s);
  1507.     if(up->ibuf != NULLBUF){
  1508.         free_p(up->ibuf);
  1509.         up->ibuf = NULLBUF;
  1510.     }
  1511.     switch(up->type){
  1512.     case TYPE_LOCAL_STREAM:
  1513.     case TYPE_LOCAL_DGRAM:
  1514.         if(up->cb.local->peer != NULLUSOCK){
  1515.             up->cb.local->peer->cb.local->peer = NULLUSOCK;
  1516.             psignal(up->cb.local->peer,0);
  1517.         }
  1518.         free_q(&up->cb.local->q);
  1519.         free(up->cb.local);
  1520.         break;
  1521.     case TYPE_TCP:
  1522.         if(up->cb.tcb != NULLTCB){    /* In case it's been reset */
  1523.             up->cb.tcb->r_upcall = trdiscard;
  1524.             /* Tell the TCP_CLOSED upcall there's no more socket */
  1525.             up->cb.tcb->user = -1;
  1526.             close_tcp(up->cb.tcb);
  1527.         }
  1528.         break;
  1529.     case TYPE_UDP:
  1530.         if(up->cb.udp != NULLUDP){
  1531.             del_udp(up->cb.udp);
  1532.         }
  1533.         break;
  1534. #ifdef    AX25
  1535.     case TYPE_AX25I:
  1536.         if(up->cb.ax25 != NULLAX25){
  1537.             /* Tell the TCP_CLOSED upcall there's no more socket */
  1538.             up->cb.ax25->user = -1;
  1539.             disc_ax25(up->cb.ax25);
  1540.         }
  1541.         break;
  1542. #ifdef notdef
  1543.     case TYPE_AX25UI:
  1544.         Axui_sock = -1;
  1545.         free_q(&Bcq);
  1546.         psignal(&Bcq,0);    /* Unblock any reads */
  1547.         break;
  1548. #endif /* notdef */
  1549. #endif
  1550. #ifdef    NETROM
  1551.     case TYPE_NETROML3:
  1552.         del_rnr(up->cb.rnr);
  1553.         break;
  1554.     case TYPE_NETROML4:
  1555.         if(up->cb.nr4 != NULLNR4CB){
  1556.             /* Tell the TCP_CLOSED upcall there's no more socket */
  1557.             up->cb.nr4->user = -1;
  1558.             disc_nr4(up->cb.nr4);
  1559.         }
  1560.         break;
  1561. #endif
  1562.     case TYPE_RAW:
  1563.         del_ip(up->cb.rip);
  1564.         break;
  1565.     default:
  1566.         errno = EOPNOTSUPP;
  1567.         return -1;
  1568.     }
  1569. #ifdef    LZW
  1570.     if(up->zout != NULLLZW || up->zin != NULLLZW)
  1571.         lzwfree(up);
  1572. #endif
  1573.     free(up->name);
  1574.     free(up->peername);
  1575.  
  1576.     up->cb.p = NULLCHAR;
  1577.     up->name = up->peername = NULLCHAR;
  1578.     up->type = NOTUSED;
  1579.     psignal(up,0);    /* Wake up anybody doing an accept() or recv() */
  1580.     return 0;
  1581. }
  1582. /* Increment reference count for specified socket */
  1583. int
  1584. usesock(s)
  1585. int s;
  1586. {
  1587.     struct usock *up;
  1588.  
  1589.     if((up = itop(s)) == NULLUSOCK){
  1590.         errno = EBADF;
  1591.         return -1;
  1592.     }
  1593.     up->refcnt++;
  1594.     return 0;
  1595. }
  1596. /* Blow away all sockets belonging to a certain process. Used by killproc(). */
  1597. void
  1598. freesock(pp)
  1599. struct proc *pp;
  1600. {
  1601.     register struct usock *up;
  1602.     register int i;
  1603.  
  1604.     for(i=SOCKBASE;i < Nusock+SOCKBASE;i++){
  1605.         up = itop(i);
  1606.         if(up != NULLUSOCK && up->type != NOTUSED && up->owner == pp)
  1607.             shutdown(i,2);
  1608.     }
  1609. }
  1610. /* Start of internal subroutines */
  1611.  
  1612. /* Raw IP receive upcall routine */
  1613. static void
  1614. rip_recv(rp)
  1615. struct raw_ip *rp;
  1616. {
  1617.     psignal(itop(rp->user),1);
  1618.     pwait(NULL);
  1619. }
  1620.  
  1621. /* UDP receive upcall routine */
  1622. static void
  1623. s_urcall(iface,udp,cnt)
  1624. struct iface *iface;
  1625. struct udp_cb *udp;
  1626. int cnt;
  1627. {
  1628.     psignal(itop(udp->user),1);
  1629.     pwait(NULL);
  1630. }
  1631. /* TCP receive upcall routine */
  1632. static void
  1633. s_trcall(tcb,cnt)
  1634. struct tcb *tcb;
  1635. int cnt;
  1636. {
  1637.     /* Wake up anybody waiting for data, and let them run */
  1638.     psignal(itop(tcb->user),1);
  1639.     pwait(NULL);
  1640. }
  1641. /* TCP transmit upcall routine */
  1642. static void
  1643. s_ttcall(tcb,cnt)
  1644. struct tcb *tcb;
  1645. int cnt;
  1646. {
  1647.     /* Wake up anybody waiting to send data, and let them run */
  1648.     psignal(itop(tcb->user),1);
  1649.     pwait(NULL);
  1650. }
  1651. /* TCP state change upcall routine */
  1652. static void
  1653. s_tscall(tcb,old,new)
  1654. struct tcb *tcb;
  1655. int old,new;
  1656. {
  1657.     int s,ns;
  1658.     struct usock *up,*nup,*oup;
  1659.     union sp sp;
  1660.  
  1661.     s = tcb->user;
  1662.     oup = up = itop(s);
  1663.  
  1664.     switch(new){
  1665.     case TCP_CLOSED:
  1666.         /* Clean up. If the user has already closed the socket,
  1667.          * then up will be null (s was set to -1 by the close routine).
  1668.          * If not, then this is an abnormal close (e.g., a reset)
  1669.          * and clearing out the pointer in the socket structure will
  1670.          * prevent any further operations on what will be a freed
  1671.          * control block. Also wake up anybody waiting on events
  1672.          * related to this tcb so they will notice it disappearing.
  1673.          */
  1674.         if(up != NULLUSOCK){
  1675.             up->cb.tcb = NULLTCB;
  1676.             up->errcodes[0] = tcb->reason;
  1677.             up->errcodes[1] = tcb->type;
  1678.             up->errcodes[2] = tcb->code;
  1679.         }
  1680.         del_tcp(tcb);
  1681.         break;
  1682.     case TCP_SYN_RECEIVED:
  1683.         /* Handle an incoming connection. If this is a server TCB,
  1684.          * then we're being handed a "clone" TCB and we need to
  1685.          * create a new socket structure for it. In either case,
  1686.          * find out who we're talking to and wake up the guy waiting
  1687.          * for the connection.
  1688.          */
  1689.         if(tcb->flags.clone){
  1690.             /* Clone the socket */
  1691.             ns = socket(AF_INET,SOCK_STREAM,0);
  1692.             nup = itop(ns);
  1693.             ASSIGN(*nup,*up);
  1694.             tcb->user = ns;
  1695.             nup->cb.tcb = tcb;
  1696.             /* Allocate new memory for the name areas */
  1697.             nup->name = mallocw(SOCKSIZE);
  1698.             nup->peername = mallocw(SOCKSIZE);
  1699.             /* Store the new socket # in the old one */
  1700.             up->rdysock = ns;
  1701.             up = nup;
  1702.             s = ns;
  1703.         } else {
  1704.             /* Allocate space for the peer's name */
  1705.             up->peername = mallocw(SOCKSIZE);
  1706.             /* Store the old socket # in the old socket */
  1707.             up->rdysock = s;
  1708.         }
  1709.         /* Load the addresses. Memory for the name has already
  1710.          * been allocated, either above or in the original bind.
  1711.          */
  1712.         sp.p = up->name;
  1713.         sp.in->sin_family = AF_INET;
  1714.         sp.in->sin_addr.s_addr = up->cb.tcb->conn.local.address;
  1715.         sp.in->sin_port = up->cb.tcb->conn.local.port;
  1716.         up->namelen = SOCKSIZE;
  1717.  
  1718.         sp.p = up->peername;
  1719.         sp.in->sin_family = AF_INET;
  1720.         sp.in->sin_addr.s_addr = up->cb.tcb->conn.remote.address;
  1721.         sp.in->sin_port = up->cb.tcb->conn.remote.port;
  1722.         up->peernamelen = SOCKSIZE;
  1723.  
  1724.         /* Wake up the guy accepting it, and let him run */
  1725.         psignal(oup,1);
  1726.         pwait(NULL);
  1727.         break;
  1728.     default:    /* Ignore all other state transitions */
  1729.         break;
  1730.     }
  1731.     psignal(up,0);    /* In case anybody's waiting */
  1732. }
  1733. /* Discard data received on a TCP connection. Used after a receive shutdown or
  1734.  * close_s until the TCB disappears.
  1735.  */
  1736. static void
  1737. trdiscard(tcb,cnt)
  1738. struct tcb *tcb;
  1739. int cnt;
  1740. {
  1741.     struct mbuf *bp;
  1742.  
  1743.     recv_tcp(tcb,&bp,(int16)cnt);
  1744.     free_p(bp);
  1745. }
  1746. #ifdef    AX25
  1747. /* AX.25 receive upcall */
  1748. void
  1749. s_arcall(axp,cnt)
  1750. struct ax25_cb *axp;
  1751. int cnt;
  1752. {
  1753.     int ns;
  1754.     struct usock *up,*nup,*oup;
  1755.     union sp sp;
  1756.  
  1757.     up = itop(axp->user);
  1758.     /* When AX.25 data arrives for the first time the AX.25 listener
  1759.        is notified, if active. If the AX.25 listener is a server its
  1760.        socket is duplicated in the same manner as in s_tscall().
  1761.      */
  1762.     if (Axi_sock != -1 && axp->user == -1) {
  1763.         oup = up = itop(Axi_sock);
  1764.         /* From now on, use the same upcalls as the listener */
  1765.         axp->t_upcall = up->cb.ax25->t_upcall;
  1766.         axp->r_upcall = up->cb.ax25->r_upcall;
  1767.         axp->s_upcall = up->cb.ax25->s_upcall;
  1768.         if (up->cb.ax25->flags.clone) {
  1769.             /* Clone the socket */
  1770.             ns = socket(AF_AX25,SOCK_STREAM,0);
  1771.             nup = itop(ns);
  1772.             ASSIGN(*nup,*up);
  1773.             axp->user = ns;
  1774.             nup->cb.ax25 = axp;
  1775.             /* Allocate new memory for the name areas */
  1776.             nup->name = mallocw(sizeof(struct sockaddr_ax));
  1777.             nup->peername = mallocw(sizeof(struct sockaddr_ax));
  1778.             /* Store the new socket # in the old one */
  1779.             up->rdysock = ns;
  1780.             up = nup;
  1781.         } else {
  1782.             axp->user = Axi_sock;
  1783.             del_ax25(up->cb.ax25);
  1784.             up->cb.ax25 = axp;
  1785.             /* Allocate space for the peer's name */
  1786.             up->peername = mallocw(sizeof(struct sockaddr_ax));
  1787.             /* Store the old socket # in the old socket */
  1788.             up->rdysock = Axi_sock;
  1789.         }
  1790.         /* Load the addresses. Memory for the name has already
  1791.          * been allocated, either above or in the original bind.
  1792.          */
  1793.         sp.p = up->name;
  1794.         sp.ax->sax_family = AF_AX25;
  1795.         memcpy(sp.ax->ax25_addr,axp->local,AXALEN);
  1796.         memcpy(sp.ax->iface,axp->iface->name,ILEN);
  1797.         up->namelen = sizeof(struct sockaddr_ax);
  1798.  
  1799.         sp.p = up->peername;
  1800.         sp.ax->sax_family = AF_AX25;
  1801.         memcpy(sp.ax->ax25_addr,axp->remote,AXALEN);
  1802.         memcpy(sp.ax->iface,axp->iface->name,ILEN);
  1803.         up->peernamelen = sizeof(struct sockaddr_ax);
  1804.         /* Wake up the guy accepting it, and let him run */
  1805.         psignal(oup,1);
  1806.         pwait(NULL);
  1807.         return;
  1808.     }
  1809.     /* Wake up anyone waiting, and let them run */
  1810.     psignal(up,1);
  1811.     pwait(NULL);
  1812. }
  1813. /* AX.25 transmit upcall */
  1814. void
  1815. s_atcall(axp,cnt)
  1816. struct ax25_cb *axp;
  1817. int cnt;
  1818. {
  1819.     /* Wake up anyone waiting, and let them run */
  1820.     psignal(itop(axp->user),1);
  1821.     pwait(NULL);
  1822. }
  1823. /* AX25 state change upcall routine */
  1824. void
  1825. s_ascall(axp,old,new)
  1826. register struct ax25_cb *axp;
  1827. int old,new;
  1828. {
  1829.     int s;
  1830.     struct usock *up;
  1831.  
  1832.     s = axp->user;
  1833.     up = itop(s);
  1834.  
  1835.     switch(new){
  1836.     case LAPB_DISCONNECTED:
  1837.         /* Clean up. If the user has already closed the socket,
  1838.          * then up will be null (s was set to -1 by the close routine).
  1839.          * If not, then this is an abnormal close (e.g., a reset)
  1840.          * and clearing out the pointer in the socket structure will
  1841.          * prevent any further operations on what will be a freed
  1842.          * control block. Also wake up anybody waiting on events
  1843.          * related to this block so they will notice it disappearing.
  1844.          */
  1845.         if(up != NULLUSOCK){
  1846.             up->errcodes[0] = axp->reason;
  1847.             up->cb.ax25 = NULLAX25;
  1848.         }
  1849.         del_ax25(axp);
  1850.         break;
  1851.     default:    /* Other transitions are ignored */
  1852.         break;
  1853.     }
  1854.     psignal(up,0);    /* In case anybody's waiting */
  1855. }
  1856. #endif
  1857. #ifdef NETROM
  1858. /* NET/ROM receive upcall routine */
  1859. static void
  1860. s_nrcall(cb,cnt)
  1861. struct nr4cb *cb;
  1862. int cnt;
  1863. {
  1864.     /* Wake up anybody waiting for data, and let them run */
  1865.     psignal(itop(cb->user),1);
  1866.     pwait(NULL);
  1867. }
  1868. /* NET/ROM transmit upcall routine */
  1869. static void
  1870. s_ntcall(cb,cnt)
  1871. struct nr4cb *cb;
  1872. int cnt;
  1873. {
  1874.     /* Wake up anybody waiting to send data, and let them run */
  1875. /*    psignal(itop(cb->user),1); */
  1876. /* Quick-fix for sending wait problem - Dave Perry, VE3IFB */
  1877.     psignal(itop(cb->user),0);
  1878.     pwait(NULL);
  1879. }
  1880. /* NET/ROM state change upcall routine */
  1881. static void
  1882. s_nscall(cb,old,new)
  1883. struct nr4cb *cb;
  1884. int old,new;
  1885. {
  1886.     int s,ns;
  1887.     struct usock *up,*nup,*oup;
  1888.     union sp sp;
  1889.  
  1890.     s = cb->user;
  1891.     oup = up = itop(s);
  1892.  
  1893.      if(new == NR4STDISC && up != NULLUSOCK){
  1894.         /* Clean up. If the user has already closed the socket,
  1895.          * then up will be null (s was set to -1 by the close routine).
  1896.          * If not, then this is an abnormal close (e.g., a reset)
  1897.          * and clearing out the pointer in the socket structure will
  1898.          * prevent any further operations on what will be a freed
  1899.          * control block. Also wake up anybody waiting on events
  1900.          * related to this cb so they will notice it disappearing.
  1901.          */
  1902.          up->cb.nr4 = NULLNR4CB;
  1903.          up->errcodes[0] = cb->dreason;
  1904.      }
  1905.      if(new == NR4STCON && old == NR4STDISC){
  1906.         /* Handle an incoming connection. If this is a server cb,
  1907.          * then we're being handed a "clone" cb and we need to
  1908.          * create a new socket structure for it. In either case,
  1909.          * find out who we're talking to and wake up the guy waiting
  1910.          * for the connection.
  1911.          */
  1912.         if(cb->clone){
  1913.             /* Clone the socket */
  1914.             ns = socket(AF_NETROM,SOCK_SEQPACKET,0);
  1915.             nup = itop(ns);
  1916.             ASSIGN(*nup,*up);
  1917.             cb->user = ns;
  1918.             nup->cb.nr4 = cb;
  1919.             cb->clone = 0; /* to avoid getting here again */
  1920.             /* Allocate new memory for the name areas */
  1921.             nup->name = mallocw(sizeof(struct sockaddr_nr));
  1922.             nup->peername = mallocw(sizeof(struct sockaddr_nr));
  1923.             /* Store the new socket # in the old one */
  1924.             up->rdysock = ns;
  1925.             up = nup;
  1926.             s = ns;
  1927.         } else {
  1928.             /* Allocate space for the peer's name */
  1929.             up->peername = mallocw(sizeof(struct sockaddr_nr));
  1930.             /* Store the old socket # in the old socket */
  1931.             up->rdysock = s;
  1932.         }
  1933.         /* Load the addresses. Memory for the name has already
  1934.          * been allocated, either above or in the original bind.
  1935.          */
  1936.         sp.p = up->name;
  1937.         sp.nr->nr_family = AF_NETROM;
  1938.         ASSIGN(sp.nr->nr_addr,up->cb.nr4->local);
  1939.         up->namelen = sizeof(struct sockaddr_nr);
  1940.  
  1941.         sp.p = up->peername;
  1942.         sp.nr->nr_family = AF_NETROM;
  1943.         ASSIGN(sp.nr->nr_addr,up->cb.nr4->remote);
  1944.         up->peernamelen = sizeof(struct sockaddr_nr);
  1945.  
  1946.         /* Wake up the guy accepting it, and let him run */
  1947.         psignal(oup,1);
  1948.         pwait(NULL);
  1949.     }
  1950.      /* Ignore all other state transitions */    
  1951.     psignal(up,0);    /* In case anybody's waiting */
  1952. }
  1953. #endif
  1954.  
  1955. /* Verify address family and length according to the socket type */
  1956. static int
  1957. checkaddr(type,name,namelen)
  1958. int type;
  1959. char *name;
  1960. int namelen;
  1961. {
  1962.     union sp sp;
  1963.  
  1964.     sp.p = name;
  1965.     /* Verify length and address family according to protocol */
  1966.     switch(type){
  1967.     case TYPE_TCP:
  1968.     case TYPE_UDP:
  1969.         if(sp.in->sin_family != AF_INET
  1970.          || namelen != sizeof(struct sockaddr_in))
  1971.             return -1;
  1972.         break;
  1973. #ifdef    AX25
  1974.     case TYPE_AX25I:
  1975. #ifdef notdef
  1976.     case TYPE_AX25UI:
  1977. #endif
  1978.         if(sp.ax->sax_family != AF_AX25
  1979.          || namelen != sizeof(struct sockaddr_ax))
  1980.             return -1;
  1981.         break;
  1982. #endif
  1983. #ifdef    NETROM
  1984.     case TYPE_NETROML3:
  1985.     case TYPE_NETROML4:
  1986.         if(sp.nr->nr_family != AF_NETROM
  1987.          || namelen != sizeof(struct sockaddr_nr))
  1988.             return -1;
  1989.         break;
  1990. #endif
  1991.     }
  1992.     return 0;
  1993. }
  1994. /* Issue an automatic bind of a local address */
  1995. static void
  1996. autobind(s,af)
  1997. int s,af;
  1998. {
  1999.     char buf[MAXSOCKSIZE];
  2000.     union sp sp;
  2001.  
  2002.     sp.p = buf;
  2003.     switch(af){
  2004.     case AF_INET:
  2005.         sp.in->sin_family = AF_INET;
  2006.         sp.in->sin_addr.s_addr = INADDR_ANY;
  2007.         sp.in->sin_port = Lport++;
  2008.         bind(s,sp.p,sizeof(struct sockaddr_in));
  2009.         break;
  2010. #ifdef    AX25
  2011.     case AF_AX25:
  2012.         sp.ax->sax_family = AF_AX25;
  2013.         memset(sp.ax->ax25_addr,'\0',AXALEN);
  2014.         memset(sp.ax->iface,'\0',ILEN);
  2015.         bind(s,sp.p,sizeof(struct sockaddr_ax));
  2016.         break;
  2017. #endif
  2018. #ifdef    NETROM
  2019.     case AF_NETROM:
  2020.         sp.nr->nr_family = AF_NETROM;
  2021.         memcpy(sp.nr->nr_addr.user,Mycall,AXALEN);
  2022.         memcpy(sp.nr->nr_addr.node,Mycall,AXALEN);
  2023.         bind(s,sp.p,sizeof(struct sockaddr_nr));
  2024.         break;
  2025. #endif
  2026.     }
  2027. }
  2028.  
  2029. /* this is called by tipmail only ! - WG7J */
  2030. #ifdef TIPMAIL
  2031.  
  2032. /* Return a pair of mutually connected sockets in sv[0] and sv[1] */
  2033. int
  2034. socketpair(af,type,protocol,sv)
  2035. int af;
  2036. int type;
  2037. int protocol;
  2038. int sv[];
  2039. {
  2040.     struct usock *up0, *up1;
  2041.     if(sv == NULLINT){
  2042.         errno = EFAULT;
  2043.         return -1;
  2044.     }
  2045.     if(af != AF_LOCAL){
  2046.         errno = EAFNOSUPPORT;
  2047.         return -1;
  2048.     }
  2049.     if(type != SOCK_STREAM && type != SOCK_DGRAM){
  2050.         errno = ESOCKTNOSUPPORT;
  2051.         return -1;
  2052.     }
  2053.     if((sv[0] = socket(af,type,protocol)) == -1)
  2054.         return -1;
  2055.     if((sv[1] = socket(af,type,protocol)) == -1){
  2056.         close_s(sv[0]);
  2057.         return -1;
  2058.     }
  2059.     up0 = itop(sv[0]);
  2060.     up1 = itop(sv[1]);
  2061.     up0->cb.local->peer = up1;
  2062.     up1->cb.local->peer = up0;
  2063.     return sv[1];
  2064. }
  2065. #endif
  2066.  
  2067.